package com.framework.modules.sys.controller;


import com.framework.entity.sys.SysAgent;
import com.framework.exception.util.RespResult;
import com.framework.page.PageUtils;
import com.framework.service.AgentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * 合伙人相关
 */
@RestController
@RequestMapping("/sys/sysAgent")
public class SysAgentController extends AbstractController {

    @Autowired
    private AgentService agentService;

    /**
     * 合伙人列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysagent:list")
    public RespResult list(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        PageUtils page = agentService.queryAgentList(params);
        return RespResult.ok().put("page", page);
    }

    /**
     * 删除合伙人
     *
     * @param agent
     * @return
     */
    @RequestMapping("/deleteAgent")
    @RequiresPermissions("sys:sysagent:update")
    public RespResult update(@RequestBody SysAgent agent) {
        agentService.freezAgent(agent);
        return RespResult.ok();
    }

    /**
     * 冻结合伙人
     *
     * @param agent
     * @return
     */
    @RequestMapping("/freezAgent")
    @RequiresPermissions("sys:sysagent:update")
    public RespResult freeze(@RequestBody SysAgent agent) {
        agentService.freezAgent(agent);
        return RespResult.ok();
    }

    /**
     * 查询个人信息
     *
     * @param agentId
     * @return
     */
    @RequestMapping("/findDetailById")
    @RequiresPermissions("sys:sysagent:list")
    public RespResult findDetailById(Long agentId) {
        Map<String, Object> agent = agentService.findAgentDetail(agentId);
        return RespResult.ok().put("agent", agent);
    }

    /**
     * 更新合伙人
     *
     * @param agent
     * @return
     */
    @RequestMapping("/updateAgent")
    @RequiresPermissions(("sys:sysagent:update"))
    public RespResult updateAgent(@RequestBody Map<String, Object> agent) {
        agentService.updateAgent(agent);
        return RespResult.ok();
    }

    /**
     * 检查姓名账号
     *
     * @param parentPhone
     * @param parentName
     * @return
     */
    @RequestMapping("/checkParent")
    public RespResult checkParent(@RequestParam String parentPhone, @RequestParam String parentName) {
        Long deptId = getUser().getDeptId();
        Long pid = agentService.checkParent(parentPhone, parentName, deptId);
        return RespResult.ok().put("parent", pid);
    }

    /**
     * 封禁列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/forbidList")
    @RequiresPermissions("sys:sysagent:list")
    public RespResult forbidList(@RequestParam Map<String, Object> params) {
        params.put("deptId",getDeptId());
        params.put("state", 2);
        PageUtils page = agentService.queryForbidAgentList(params);
        return RespResult.ok().put("page", page);
    }


    /**
     * 封禁提现列表
     *
     * @param params
     * @return
     */
    @RequestMapping("/forbidTakeList")
    @RequiresPermissions("sys:sysagent:list")
    public RespResult forbidTakeList(@RequestParam Map<String, Object> params) {
        params.put("deptId", getDeptId());
        params.put("state", 3);
        PageUtils page = agentService.queryForbidAgentList(params);
        return RespResult.ok().put("page", page);
    }

    /**
     * 根据姓名查询
     *
     * @param name
     * @return
     */
    @RequestMapping("/byName")
    @RequiresPermissions("sys:sysagent:list")
    public RespResult queryByName(@RequestParam String name) {
        Long deptId = getUser().getDeptId();
        return RespResult.ok().put("list", agentService.queryByName(name, deptId));
    }

    /**
     * 费率等级
     * @param agentId
     * @return
     */
    @RequestMapping("/getRateInfo/{agentId}")
    public RespResult getRateList(@PathVariable Long agentId) {
        List<Map<String, Object>> rateList = agentService.getRateList(agentId, getDeptId());
        return RespResult.ok().put("rateList", rateList);
    }

    /**
     * 取消实名认证
     *
     * @param agentId
     * @return
     */
    @RequestMapping("/cancelRealName/{agentId}")
    public RespResult cancelRealName(@PathVariable Long agentId) {
        agentService.cancelRealName(agentId);
        return RespResult.ok();
    }

    /**
     * 实名
     * @param params
     * @return
     */
    @RequestMapping("realName")
    public RespResult realName(@RequestBody Map<String, Object> params) {
        agentService.realNameByAdmin(params, getDeptId());
        return RespResult.ok();
    }

    /**
     * 下载
     * @param response
     * @return
     */
    @RequestMapping("down")
    public RespResult test(HttpServletResponse response) {
        agentService.downLoad(getDeptId(),response);
        return RespResult.ok();
    }

}
